﻿import com.crystalcentral.model.*;
import com.crystalcentral.view.*;
import com.crystalcentral.util.*;
import com.crystalcentral.*;

import mx.utils.Delegate;

import com.darronschall.DynamicRegistration;

class com.crystalcentral.view.MText extends com.crystalcentral.view.MItem
{
	// Constants:
	public static var CLASS_REF = com.crystalcentral.view.MText;
	
	var diyTextItem:DiyTextItem;
	var tf:TextField;
	var fontItem:FontItem;
	
	var editable:Boolean;
	
	var oldDepth:Number;
	
	public function MText(diyItem:DiyTextItem, mCanvas:MCanvas)
	{
		// constructor code
		super(diyItem,mCanvas);
		this.diyTextItem = diyItem;
		
		this.fontItem = FontLoader.getInstance().getFontItemByLabel(diyItem.fontName);

		this.tf = this.mcContent.createTextField("tf", this.mcContent.getNextHighestDepth(), 0, 0, diyItem.width, diyItem.height);
		var fmt:TextFormat = new TextFormat();
		fmt.font = fontItem.fontName;
		fmt.color = diyItem.color;
		fmt.size = diyItem.fontSize;
		fmt.bold = diyItem.bold;
		fmt.italic = diyItem.italic;
		fmt.underline = diyItem.underline;
		fmt.align = diyItem.align;
		fmt.letterSpacing = diyItem.letterSpacing;
		fmt.leading = diyItem.leading;
		this.tf.wordWrap = diyItem.wordWrap;
		if (diyItem.wordWrap) {
			this.tf._width = diyItem.width / diyItem.xscale * 100;
		}
		tf.setNewTextFormat(fmt);
		
		tf.borderColor = 0x0000FF;
		tf.autoSize = true;
		tf.multiline = true;
		//tf.wordWrap = true;
		tf.type = "input";
		
		tf.text = StringTool.replace(diyItem.text,"\r\n",newline);
		
		tf.onSetFocus = Delegate.create(this. onTextFocus);
		tf.onChanged = Delegate.create(this, onTextChanged);
		
		loadEmbedFont(diyItem);
	}
	
	function loadEmbedFont(diyItem:DiyTextItem)
	{
		_root.diyTool.startLoading("Loading Embed Font...");
		
		var fontLoader:FontLoader = FontLoader.getInstance();
		var fontItem:FontItem = fontLoader.getFontItemByLabel(diyItem.fontName);
		var fontFile:String = fontLoader.getFontFile(fontItem, diyItem);

		var nDepth = _root.getNextHighestDepth();
		var mcFont:MovieClip = _root.createEmptyMovieClip("mcFont_" + nDepth, nDepth);

		var mcl:MovieClipLoader = new MovieClipLoader();
		var listener = new Object();
		listener.onLoadInit = Delegate.create(this, onLoadFontInit);
		listener.onLoadError = function() {
			trace("loadEmbedFont: onLoadError");
			_root.diyTool.stopLoading();
		}
		mcl.addListener(listener);

		trace("Load embed font: " + fontFile);
		mcl.loadClip(fontFile,mcFont);
	}
	
	function onLoadFontInit()
	{
		//trace("Font Loaded: " + this.tf);
		this.tf.embedFonts = true;
		begineRefreshItem();	// refresh to enforce display embedfonts
		
		_root.diyTool.stopLoading();
	}
	
	function setOnFocus() {
		super.setOnFocus();
		_root.cpicker.color = this.getFontColor();
		_root.cpicker.fontColor.setColorThumb(this.getFontColor());
		
		_root.txtFontName.text = this.diyTextItem.fontName;
		_root.cbFontList.selectedIndex = this.getFontNameIndex();
		_root.txtFontSize.text = this.diyTextItem.fontSize;
	}
	function setLostFocus() {
		super.setLostFocus();
		
		if (this.editable)
			this.mcItem.swapDepths(this.oldDepth);
		
		this.setEditMode(false);
		Selection.setSelection(0,0);
	}
	function getFontNameIndex() {
		var currentFontName = String(this.diyTextItem.fontName);
		var iLen = _root.cbFontList.length;
		for (var i = 0; i < iLen; i++)
		{
			var lbl = String(_root.cbFontList.getItemAt(i).fname);
			if (lbl == currentFontName)
			{
				return i;
			}
		}
		return -1;
	};
	function drawBorder() {
		super.drawBorder();
	}
	
	function onItemPress() {
		super.onItemPress();
		DiyTool.getInstance().setTextTools(this);
	}
	
	function onItemRelease() {
		super.onItemRelease();
	}
	
	function onItemReleaseOutside() {
		super.onItemReleaseOutside();
	}
	
	function onItemDoubleClick() {
		super.onItemDoubleClick();
		this.setEditMode(true);
		this.oldDepth = this.mcItem.getDepth();
		this.mcItem.swapDepths(mCanvas.canvas.getNextHighestDepth());
		
		this.lastStatus = new DiyTextItem();
		this.lastStatus.text = StringTool.replace(diyItem.text,"\r\n",newline);;
	}
	
	function onTextChanged() {
		this.mcItem.setRegistration(this.mcContent._width/2, this.mcContent._height/2);
		this.diyItem.text = this.tf.text;
		this.showBorder();
	}
	
	function onTextFocus() {
		mCanvas.currentItem = this;
	}
	
	function setEditMode(editable:Boolean) {
		if (editable) {
			this.removeClickEvent();
			this.editable = true;
			//this.tf.type = "input";
			this.tf.selectable = true;
			this.hideEditTools();
		}
		else {
			this.registerClickEvent();
			this.editable = false;
			//this.tf.type = "dynamic";
			this.tf.selectable = false;
			
			if (this.lastStatus != undefined && this.lastStatus.text != undefined && this.diyItem.text != this.lastStatus.text) {
				trace(this.diyItem.text + "\n" + this.lastStatus.text)
				this.aUndoDiyItem.push(this.lastStatus);
				
				_root.addToUndo(this, function (isUndo:Boolean)
				{
					if (isUndo)
					{
						var undoDiyItem = this.aUndoDiyItem.pop();
						this.aRedoDiyItem.push(this.diyTextItem.clone());
						
						this.diyItem.text = undoDiyItem.text;
						this.tf.text = undoDiyItem.text;
						
						this.setOnFocus();
					}
					else
					{
						var redoDiyItem = this.aRedoDiyItem.pop();
						this.aUndoDiyItem.push(this.diyTextItem.clone());
						
						this.diyItem.text = redoDiyItem.text;
						this.tf.text = redoDiyItem.text;
						
						this.setOnFocus();
					}
				});
			}
			lastStatus = undefined;
		}
	}
	
	function setFont(fontLabel:String) {
		if (this.diyTextItem.fontName != fontLabel) {
			var undoDiyItem = this.diyTextItem.clone();
			this.aUndoDiyItem.push(undoDiyItem);
			
			_root.addToUndo(this, function (isUndo:Boolean)
			{
				if (isUndo)
				{
					var undoDiyItem = this.aUndoDiyItem.pop();
					this.aRedoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.fontName = undoDiyItem.fontName;
					this.fontItem = FontLoader.getInstance().getFontItemByLabel(this.diyTextItem.fontName);
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.font = fontItem.fontName;
					tf.setTextFormat(fmt);
					
					loadEmbedFont(diyTextItem);
				}
				else
				{
					var redoDiyItem = this.aRedoDiyItem.pop();
					this.aUndoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.fontName = redoDiyItem.fontName;
					this.fontItem = FontLoader.getInstance().getFontItemByLabel(this.diyTextItem.fontName);
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.font = fontItem.fontName;
					tf.setTextFormat(fmt);
					
					loadEmbedFont(diyTextItem);
				}
			});
		}
		
		this.diyTextItem.fontName = fontLabel;
		
		this.fontItem = FontLoader.getInstance().getFontItemByLabel(fontLabel);
		var fontFile:String = fontLoader.getFontFile(fontItem, this.diyTextItem);
		
		var fmt:TextFormat = this.tf.getTextFormat();
		fmt.font = fontItem.fontName;
		fmt.bold = diyTextItem.bold;
		fmt.italic = diyTextItem.italic;
		fmt.underline = diyTextItem.underline;
		
		if (fontFile == undefined) {
			fmt.bold = false;
			fmt.italic = false;
			this.diyTextItem.bold = false;
			this.diyTextItem.italic = false;
		}
		
		tf.setTextFormat(fmt);
		
		loadEmbedFont(diyTextItem);
	}
	
	function getFontSize() {
		var fmt = this.tf.getTextFormat();
		return fmt.size;
	}
	function setFontSize(fSize:Number) {
		if (fSize < 1){
			return;
		}
		else {
			var undoDiyItem = this.diyTextItem.clone();
			this.aUndoDiyItem.push(undoDiyItem);
				
			_root.addToUndo(this, function (isUndo:Boolean)
			{
				if (isUndo)
				{
					var undoDiyItem = this.aUndoDiyItem.pop();
					this.aRedoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.fontSize = undoDiyItem.fontSize;
				
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.size = this.diyTextItem.fontSize;
					tf.setTextFormat(fmt);
					this.setOnFocus();
				}
				else
				{
					var redoDiyItem = this.aRedoDiyItem.pop();
					this.aUndoDiyItem.push(this.diyTextItem.clone());
						
					this.diyTextItem.fontSize = redoDiyItem.fontSize;
				
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.size = this.diyTextItem.fontSize;
					tf.setTextFormat(fmt);
					this.setOnFocus();
				}
			});
			
			
			
			this.diyTextItem.fontSize = fSize;
				
			var fmt:TextFormat = this.tf.getTextFormat();
			fmt.size = this.diyTextItem.fontSize;
			tf.setTextFormat(fmt);
			this.setOnFocus();
		}
	}
	function setBold(isBold:Boolean) {
		if ((isBold && this.diyTextItem.italic && this.fontItem.fBoldItalic == undefined)
			|| (isBold && !this.diyTextItem.italic && this.fontItem.fBold == undefined))
		{
				_root.showMessage("Sorry, this font does not have Bold capabilities.");
				return;
		}
		else {
			if (this.diyTextItem.bold != isBold) {
				var undoDiyItem = this.diyTextItem.clone();
				this.aUndoDiyItem.push(undoDiyItem);
				
				_root.addToUndo(this, function (isUndo:Boolean)
				{
					if (isUndo)
					{
						var undoDiyItem = this.aUndoDiyItem.pop();
						this.aRedoDiyItem.push(this.diyTextItem.clone());
						
						this.diyTextItem.bold = undoDiyItem.bold;
						
						var fmt:TextFormat = this.tf.getTextFormat();
						fmt.bold = this.diyTextItem.bold;
						tf.setTextFormat(fmt);
						
						loadEmbedFont(diyTextItem);
					}
					else
					{
						var redoDiyItem = this.aRedoDiyItem.pop();
						this.aUndoDiyItem.push(this.diyTextItem.clone());
						
						this.diyTextItem.bold = redoDiyItem.bold;
						
						var fmt:TextFormat = this.tf.getTextFormat();
						fmt.bold = this.diyTextItem.bold;
						tf.setTextFormat(fmt);
						
						loadEmbedFont(diyTextItem);
					}
				});
			}
			
			
			this.diyTextItem.bold = isBold;
				
			var fmt:TextFormat = this.tf.getTextFormat();
			fmt.bold = this.diyTextItem.bold;
			tf.setTextFormat(fmt);
			loadEmbedFont(this.diyTextItem);
		}
	}
	function setItalic(isItalic:Boolean) {
		if ((isItalic && this.diyTextItem.bold && this.fontItem.fBoldItalic == undefined)
			|| (isItalic && !this.diyTextItem.bold && this.fontItem.fItalic == undefined)) 
		{
				_root.showMessage("Sorry, this font does not have Italic capabilities.");
				return;
		}
		else {
			if (this.diyTextItem.italic != isItalic) {
				var undoDiyItem = this.diyTextItem.clone();
				this.aUndoDiyItem.push(undoDiyItem);
				
				_root.addToUndo(this, function (isUndo:Boolean)
				{
					if (isUndo)
					{
						var undoDiyItem = this.aUndoDiyItem.pop();
						this.aRedoDiyItem.push(this.diyTextItem.clone());
						
						this.diyTextItem.italic = undoDiyItem.italic;
						
						var fmt:TextFormat = this.tf.getTextFormat();
						fmt.italic = this.diyTextItem.italic;
						tf.setTextFormat(fmt);
						
						loadEmbedFont(diyTextItem);
					}
					else
					{
						var redoDiyItem = this.aRedoDiyItem.pop();
						this.aUndoDiyItem.push(this.diyTextItem.clone());
						
						this.diyTextItem.italic = redoDiyItem.italic;
						
						var fmt:TextFormat = this.tf.getTextFormat();
						fmt.italic = this.diyTextItem.italic;
						tf.setTextFormat(fmt);
						
						loadEmbedFont(diyTextItem);
					}
				});
			}
			
			this.diyTextItem.italic = isItalic;
				
			var fmt:TextFormat = this.tf.getTextFormat();
			fmt.italic = this.diyTextItem.italic;
			tf.setTextFormat(fmt);
			loadEmbedFont(this.diyTextItem);
		}
	}
	function setUnderline(isUnderline:Boolean) {
		if (this.diyTextItem.underline != isUnderline) {
			var undoDiyItem = this.diyTextItem.clone();
			this.aUndoDiyItem.push(undoDiyItem);
			
			_root.addToUndo(this, function (isUndo:Boolean)
			{
				if (isUndo)
				{
					var undoDiyItem = this.aUndoDiyItem.pop();
					this.aRedoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.underline = undoDiyItem.underline;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.underline = diyTextItem.underline;
					tf.setTextFormat(fmt);
				}
				else
				{
					var redoDiyItem = this.aRedoDiyItem.pop();
					this.aUndoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.underline = redoDiyItem.underline;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.underline = diyTextItem.underline;
					tf.setTextFormat(fmt);
				}
			});
		}
		
		this.diyTextItem.underline = isUnderline;
				
		var fmt:TextFormat = this.tf.getTextFormat();
		fmt.underline = this.diyTextItem.underline;
		tf.setTextFormat(fmt);
		showBorder();
	}
	function setFontColor(color:Number)
	{
		if (this.diyTextItem.color != color) {
			var undoDiyItem = this.diyTextItem.clone();
			this.aUndoDiyItem.push(undoDiyItem);
			
			_root.addToUndo(this, function (isUndo:Boolean)
			{
				if (isUndo)
				{
					var undoDiyItem = this.aUndoDiyItem.pop();
					this.aRedoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.color = undoDiyItem.color;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.color = diyTextItem.color;
					tf.setTextFormat(fmt);
				}
				else
				{
					var redoDiyItem = this.aRedoDiyItem.pop();
					this.aUndoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.color = redoDiyItem.color;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.color = diyTextItem.color;
					tf.setTextFormat(fmt);
				}
			});
		}
		
		this.diyTextItem.color = color;
		
		var fmt = this.tf.getTextFormat();
		fmt.color = this.diyTextItem.color;
		this.tf.setTextFormat(fmt);
	};
	function getFontColor()
	{
		return this.tf.getTextFormat().color;
	};
	
	function setAlign(align:String) {
		if (align != "left" && align != "right" && align != "center" && align != "justify")
			align = "left";
		
		if (this.diyTextItem.align != align) {
			var undoDiyItem = this.diyTextItem.clone();
			this.aUndoDiyItem.push(undoDiyItem);
			
			_root.addToUndo(this, function (isUndo:Boolean)
			{
				if (isUndo)
				{
					var undoDiyItem = this.aUndoDiyItem.pop();
					this.aRedoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.align = undoDiyItem.align;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.align = diyTextItem.align;
					tf.setTextFormat(fmt);
				}
				else
				{
					var redoDiyItem = this.aRedoDiyItem.pop();
					this.aUndoDiyItem.push(this.diyTextItem.clone());
					
					this.diyTextItem.align = redoDiyItem.align;
					
					var fmt:TextFormat = this.tf.getTextFormat();
					fmt.align = diyTextItem.align;
					tf.setTextFormat(fmt);
				}
			});
		}
		
		
		this.diyTextItem.align = align;
				
		var fmt:TextFormat = this.tf.getTextFormat();
		fmt.align = this.diyTextItem.align;
		tf.setTextFormat(fmt);
		showBorder();
	}
	function increaseLetterSpacing() {
		var fmt:TextFormat = this.tf.getTextFormat();
		//if (fmt.letterSpacing >= 10) return;
		
		fmt.letterSpacing++;
		tf.setTextFormat(fmt);
		this.diyTextItem.letterSpacing = fmt.letterSpacing;
		this.setOnFocus();
		
		_root.addToUndo(this, function (isUndo:Boolean)
		{
			if (isUndo)
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.letterSpacing--;
				tf.setTextFormat(fmt);
				this.diyTextItem.letterSpacing = fmt.letterSpacing;
				this.setOnFocus();
			}
			else
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.letterSpacing++;
				tf.setTextFormat(fmt);
				this.diyTextItem.letterSpacing = fmt.letterSpacing;
				this.setOnFocus();
			}
		});
	}
	function decreaseLetterSpacing() {
		var fmt:TextFormat = this.tf.getTextFormat();
		//if (fmt.letterSpacing <= -10) return;
		
		fmt.letterSpacing--;
		tf.setTextFormat(fmt);
		this.diyTextItem.letterSpacing = fmt.letterSpacing;
		this.setOnFocus();
		
		_root.addToUndo(this, function (isUndo:Boolean)
		{
			if (isUndo)
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.letterSpacing++;
				tf.setTextFormat(fmt);
				this.diyTextItem.letterSpacing = fmt.letterSpacing;
				this.setOnFocus();
			}
			else
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.letterSpacing--;
				tf.setTextFormat(fmt);
				this.diyTextItem.letterSpacing = fmt.letterSpacing;
				this.setOnFocus();
			}
		});
	}
	function increaseLeading() {
		var fmt:TextFormat = this.tf.getTextFormat();
		//if (fmt.leading >= 10) return;
		
		fmt.leading++;
		tf.setTextFormat(fmt);
		this.diyTextItem.leading = fmt.leading;
		this.setOnFocus();
		
		_root.addToUndo(this, function (isUndo:Boolean)
		{
			if (isUndo)
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.leading--;
				tf.setTextFormat(fmt);
				this.diyTextItem.leading = fmt.leading;
				this.setOnFocus();
			}
			else
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.leading++;
				tf.setTextFormat(fmt);
				this.diyTextItem.leading = fmt.leading;
				this.setOnFocus();
			}
		});
	}
	function decreaseLeading() {
		var fmt:TextFormat = this.tf.getTextFormat();
		//if (fmt.leading <= -10) return;
		
		fmt.leading--;
		tf.setTextFormat(fmt);
		this.diyTextItem.leading = fmt.leading;
		this.setOnFocus();
		
		_root.addToUndo(this, function (isUndo:Boolean)
		{
			if (isUndo)
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.leading++;
				tf.setTextFormat(fmt);
				this.diyTextItem.leading = fmt.leading;
				this.setOnFocus();
			}
			else
			{
				var fmt:TextFormat = this.tf.getTextFormat();
				fmt.leading--;
				tf.setTextFormat(fmt);
				this.diyTextItem.leading = fmt.leading;
				this.setOnFocus();
			}
		});
	}
	
	function initEditToolActions() {
		super.initEditToolActions();
	}
}